// 导入数据库
const db = require('../db/index')

// 登录日志记录 account,name,email =>
exports.loginLog = (req, res) => {
    const { account, name, email } = req.body
    const login_time = new Date()
    const sql1 = "insert into login_log set ?"
    db.query(sql1, { account, name, email, login_time }, (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '登录日志更新成功' })
    })
}

// 通过姓名去搜索最近十条登录日志 name =>
exports.searchLoginLogByName = (req, res) => {
    const { name } = req.body
    const sql1 = `select * from login_log where name like '%${name}%' order by login_time DESC limit 10`
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 登录日志分页管理：==================================================
// 登录日志数量 无 =>
exports.getLoginLogNumber = (req, res) => {
    const sql1 = "select * from login_log"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}

// 登录日志监听换页 pager =>
exports.returnLoginLogData = (req, res) => {
    const { pager } = req.body
    const number = (pager - 1) * 10
    const sql1 = `select * from login_log order by login_time limit 10 offset ${number}`
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 清空登录日志 无 =>
exports.clearLoginLog = (req, res) => {
    const sql1 = "truncate table login_log"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '清空登录日志成功' })
    })
}
//=============================================================================================
// 操作日志记录 operation_person,operation_content,operation_level =>
exports.operationLog = (req, res) => {
    const { operation_person, operation_content, operation_level } = req.body
    const operation_time = new Date()
    const sql1 = "insert into operation_log set ?"
    db.query(sql1, { operation_person, operation_content, operation_level, operation_time }, (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '操作日志更新成功' })
    })
}

// 通过操作人去搜索最近十条操作日志 operation_person =>
exports.searchOperationLogByPerson = (req, res) => {
    const { operation_person } = req.body
    const sql1 = `select * from operation_log where operation_person like '%${operation_person}%' order by operation_time DESC limit 10`
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 操作日志分页管理：==================================================
// 操作日志数量 无 =>
exports.getOperationLogNumber = (req, res) => {
    const sql1 = "select * from operation_log"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({
            length: result.length
        })
    })
}

// 操作日志监听换页 pager =>
exports.returnOperationLogData = (req, res) => {
    const { pager } = req.body
    const number = (pager - 1) * 10
    const sql1 = `select * from operation_log order by operation_time limit 10 offset ${number}`
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 清空操作日志 无 =>
exports.clearOperationLog = (req, res) => {
    const sql1 = "truncate table operation_log"
    db.query(sql1, (err, result) => {
        if (err) return res.cc(err)
        res.send({ status: 0, message: '清空操作日志成功' })
    })
}